<cfcomponent extends="assets.calgrants.com.Base">

	<cfscript>
        import assets.calgrants.com.service.WorkshopService;
		this.service = new WorkshopService();
	</cfscript>

    <cffunction name="get" output="false" access="remote" returntype="assets.calgrants.com.bo.Workshop">
        <cfreturn this.service.get(argumentCollection = arguments) />
    </cffunction>

    <cffunction name="read" output="false" access="remote" returntype="array">
        <cfreturn super.toVoArray("com.bo.Workshop", this.service.read(argumentCollection = arguments)) />
    </cffunction>
	
	<cffunction name="query" output="false" access="public" returntype="query">
		<cfreturn this.service.read(argumentCollection = arguments) />
	</cffunction>
	
	<cffunction name="save" output="false" access="public" returntype="any" hint="If a property is not present in the submission, will use saved value, or default for new">
        <cfscript>
			var workshop = "";
			var workshop_ = this.service.get(Workshop_ID = arguments.Workshop_ID);
			if (IsInstanceOf(arguments[1], "assets.csac.com.bo.Workshop")) {
				workshop = arguments[1];
			} else {
				var args = {};
				StructAppend(args, arguments);
				args.obj = Duplicate(workshop_);
				workshop = this.service.load(argumentCollection = args);
			}
			// validation
			
			// **********
			workshop.setWorkshop_ID(this.service.save(workshop));
			return workshop;
        </cfscript>
	</cffunction>
	
	<cffunction name="delete" output="false" access="public" returntype="boolean">
		<cfreturn this.service.destroy(argumentCollection = arguments) />
	</cffunction>
	
	<cffunction name="search" output="false" access="public" returntype="query" hint="Search returns approved workshops, defaults top 50 future workshops">
		<cfargument name="regionID" type="string" required="no" default="" />
		<cfargument name="region" type="string" required="no" default="" />
		<cfargument name="countyID" type="string" required="no" default="" />
		<cfargument name="county" type="string" required="no" default="" />
		<cfargument name="zipCodeID" type="string" required="no" default="" />
		<cfargument name="zipCode" type="string" required="no" default="" />
		<cfargument name="cityID" type="string" required="no" default="" />
		<cfargument name="city" type="string" required="no" default="" />
		<cfargument name="latitude" type="string" required="no" default="" />
		<cfargument name="longitude" type="string" required="no" default="" />
		<cfargument name="radiusInMiles" type="string" required="no" default="50" />
		<cfargument name="minDate" type="string" required="no" default="#DateFormat(Now(), 'm/d/yyyy')#" />
		<cfargument name="maxRows" type="string" required="no" default="50" />
		<cfargument name="orderBy" type="string" required="no" default="date" />
		<cfscript>
			var searchParams = Duplicate(arguments);
			var recs = "";
			if (IsValid("zipcode", searchParams.zipCode)) {
				var zipCodeGateway = new assets.calgrants.com.gateway.ZipCodeGateway();
				local.zipCode = zipCodeGateway.query(Zip_Code = searchParams.zipCode);
				searchParams.latitude = local.zipCode.latitude;
				searchParams.longitude = local.zipCode.longitude;
				searchParams.zipCode = "";
			}
		</cfscript>
		<cfstoredproc datasource="#getDatasource()#" procedure="searchWorkshop">
			<cfprocparam value="#searchParams.regionID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(searchParams.regionID)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.region#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(searchParams.region)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.countyID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(searchParams.countyID)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.county#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(searchParams.county)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.zipCodeID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(searchParams.zipCodeID)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.zipCode#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(searchParams.zipCode)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.cityID#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(searchParams.cityID)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.city#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(searchParams.city)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.latitude#" cfsqltype="CF_SQL_FLOAT" null="#IIf(Len(Trim(searchParams.latitude)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.longitude#" cfsqltype="CF_SQL_FLOAT" null="#IIf(Len(Trim(searchParams.longitude)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.radiusInMiles#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(searchParams.radiusInMiles)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.minDate#" cfsqltype="CF_SQL_DATE" null="#IIf(Len(Trim(searchParams.minDate)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.maxRows#" cfsqltype="CF_SQL_INTEGER" null="#IIf(Len(Trim(searchParams.maxRows)) eq 0, true, false)#" />
			<cfprocparam value="#searchParams.orderBy#" cfsqltype="CF_SQL_VARCHAR" null="#IIf(Len(Trim(searchParams.orderBy)) eq 0, true, false)#" />
			<cfprocresult name="recs" />
		</cfstoredproc>
		<cfreturn recs />
	</cffunction>

</cfcomponent>